﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DomusDotNet.Sbm.Client.SL4noCM.Infrastructure;
using DomusDotNet.Sbm.Client.SL4noCM.WCFServices;
using System.Collections.Generic;
using System.ComponentModel;

namespace DomusDotNet.Sbm.Client.SL4noCM.ViewModels
{
    public class SearchViewModel :ViewModelBase 
    {
        public SearchViewModel()
        {
            if (!DesignerProperties.IsInDesignTool)
                if (webService != null)
                { 
                    webService.SearchBooksCompleted += new EventHandler<SearchBooksCompletedEventArgs>(SearchBooksCompleted);
                 }
            Clear();
        }

        
        private void SearchBooksCompleted(object sender, SearchBooksCompletedEventArgs e)
        {
            this.WebRequestExecution = false;
            if (e.Error != null)
            {
                ErrorMessage = e.Error.Message;
            }
            else
            {
                if (e.Result.Result)
                {
                    this.Result=new ObservableCollection<BookBase>(e.Result.Books );
                    ErrorMessage = string.Empty;
                }
                else
                {
                    ErrorMessage = e.Result.Errors[0].Message;
                }
            }
        }

        private SearchBooksRequest criteria;

        public string TitleFilter
        {
            get
            {
                return criteria.TitleFilter ;
            }
            set
            {
                if (criteria.TitleFilter != value)
                {
                    criteria.TitleFilter = value;
                    OnPropertyChanged("TitleFilter");
                    ErrorMessage = string.Empty;
                }
            }
        }

        public string AuthorFilter
        {
            get
            {
                return criteria.AuthorFilter ;
            }
            set
            {
                if (criteria.AuthorFilter != value)
                {
                    criteria.AuthorFilter = value;
                    OnPropertyChanged("AuthorFilter");
                    ErrorMessage = string.Empty;
                }
            }
        }

        public DateTime? PublishDateFromFilter
        {
            get
            {
                return criteria.PublishDateFromFilter ;
            }
            set
            {
                if (criteria.PublishDateFromFilter != value)
                {
                    criteria.PublishDateFromFilter = value;
                    OnPropertyChanged("PublishDateFromFilter");
                    ErrorMessage = string.Empty;
                }
            }
        }

        public DateTime? PublishDateToFilter
        {
            get
            {
                return criteria.PublishDateToFilter;
            }
            set
            {
                if (criteria.PublishDateToFilter != value)
                {
                    criteria.PublishDateToFilter = value;
                    OnPropertyChanged("PublishDateToFilter");
                    ErrorMessage = string.Empty;
                }
            }
        }

        private ObservableCollection<BookBase> result=new ObservableCollection<BookBase>();
        public ObservableCollection<BookBase> Result
        {
            get
            {
                return result;
            }
            set
            {
                result = value;
                OnPropertyChanged("Result");
                ErrorMessage = string.Empty;
            }
        }

        private BookBase selectedBook;
        public BookBase SelectedBook
        {
            get
            {
                return selectedBook;
            }
            set
            {
                selectedBook = value;
                OnPropertyChanged("SelectedBook");
                ErrorMessage = string.Empty;
            }
        }

        public void Clear()
        {
            criteria = new SearchBooksRequest();
            Result.Clear();
            OnPropertyChanged(null);
        }

        private RelayCommand searchCommand;
        public ICommand SearchCommand
        {
            get
            {
                if (searchCommand == null)
                {
                    searchCommand = new RelayCommand((a) => Search(null),
                        (b) => CanSearch(null));
                }
                return searchCommand;
            }
        }

        public bool CanSearch(object obj)
        {
            return true;
        }

        public void Search(object obj)
        {
            ErrorMessage = string.Empty;
            criteria.SessionGUID = App.SecurityContext.SessionGUID;
            webService.SearchBooksAsync(criteria);
            WebRequestExecution = true;
        }

        

        private RelayCommand clearCommand;

        public ICommand ClearCommand
        {
            get
            {
                if (clearCommand == null)
                {
                    clearCommand = new RelayCommand((a) => Clear());
                }
                return clearCommand;
            }
        }


    }
}
